package com.feishi.project.test;

import javax.swing.*;
import java.awt.*;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2018-11-26
 * Time: 上午10:03
 */
public class A {


    public static void main(String[] args) throws InterruptedException {
        int x = 70;
        int y = 70;
        JFrame jFrame = new JFrame();
        JPanel chessboard = new JPanel();
        GridLayout grid = new GridLayout(x, y);
        Label[][] label = new Label[x][y];
        int[][] out = new int[x][y];


        chessboard.setLayout(grid);
        jFrame.add(chessboard, BorderLayout.CENTER);
        jFrame.setBounds(x, y, 600, 600);

        for (int i = 0; i < label.length; i++) {
            for (int j = 0; j < label[i].length; j++) {
                label[i][j] = new Label();
                out[i][j] = Math.abs(i+j-x)<=5&&Math.abs(i-j)<=5&&((int) (Math.random() * 20)) % 20 != 1 ? 1 : 0;
                label[i][j].setBackground(out[i][j] == 1 ? Color.WHITE : Color.black);
                chessboard.add(label[i][j]);
            }
        }
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        while (true) {

            for (int i = 0; i < out.length; i++) {
                for (int j = 0; j < out[i].length; j++) {
                    int sum = 0;
                    for (int k = 0; k < 15; k++) {
                        int a = k / 5 - 1 + i;
                        int b = k % 5 - 2 + j;
                        if (a >= 0 && a < x && b >= 0 && b < y && (a != i || b != j)) {
                            sum += out[a][b];
                        }else {
                            sum++;
                        }
                    }
                    out[i][j] = sum == 3 ? 1 : (sum == 2 ? out[i][j] : 0);
//                    out[i][j] = sum == 3 ? 1 : (sum == 2 ? out[i][j] : 0);
//                    out[i][j] = sum %7==0 ? 1 :(sum % 2 ==0 ? out[i][j] : 0) ;


                    label[i][j].setBackground(out[i][j] == 1 ? Color.WHITE : Color.black);
                }
            }
            for (int i = 0; i < label.length; i++) {
                for (int j = 0; j < label[i].length; j++) {
                    out[i][j] = label[i][j].getBackground() == Color.WHITE ? 1 : 0;
                }
            }
            Thread.sleep(100);
        }
    }
}
